package com.study.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.exception.DigitalException;
import com.study.login.service.LoginService;
import com.study.mapper.LoginMapper;
import com.study.mapper.SysUserInfoMapper;
import com.study.model.dto.LoginDTO;
import com.study.model.dto.RegisterDTO;
import com.study.model.user.SysUserInfo;
import com.study.model.vo.LoginResultVO;
import com.study.utils.JwtUtil;
import com.study.utils.RedisCache;
import com.study.utils.Result;
import com.study.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, SysUserInfo> implements LoginService {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Override
    public LoginResultVO DealloginByUserNameAndPassword(LoginDTO userNamePasswordLoginVo) {
        String username = userNamePasswordLoginVo.getUsername();
        String password = userNamePasswordLoginVo.getPassword();
        //1、 检查用户名是否存在
        SysUserInfo sysUserInfo = this.getOne(new QueryWrapper<SysUserInfo>().eq("username", username));
        if (sysUserInfo == null) {
            throw new DigitalException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        // 2、检查用户的状态
        if (sysUserInfo.getStatus() == 0) {
            throw new DigitalException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //3、登录密码加密,验证密码
        String encodePassword = encryptPassword(password);
        if (!sysUserInfo.getPassword().equals(encodePassword)) {
            throw new DigitalException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        // 4、生成token
        String token = jwtUtil.generateToken(sysUserInfo.getId(), sysUserInfo.getUsername());
        LoginResultVO loginResultVO = new LoginResultVO(username, sysUserInfo, token);
        // 存入redis,login-username-id
        redisCache.setCacheObject("login-" + username +"-"+ sysUserInfo.getId(), loginResultVO);
        return loginResultVO;
    }

    @Override
    public void RegisterByUserNameAndPassword(RegisterDTO registerDTO) {
        // 实现使用账户密码的注册操作
        // 1、检查用户名是否已经存在
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getUsername, registerDTO.getUsername());
        SysUserInfo existSysUserInfo = sysUserInfoMapper.selectOne(queryWrapper);
        if (existSysUserInfo != null) {
            throw new DigitalException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        // 2、插入新用户
        SysUserInfo newSysUserInfo = new SysUserInfo();
        newSysUserInfo.setUsername(registerDTO.getUsername());
        newSysUserInfo.setPassword(encryptPassword(registerDTO.getPassword())); // 对密码进行加密处理
        //newSysUserInfo.setStatus(1);// 默认设置账户是正常的,就是允许账户是可以使用的
        //newSysUserInfo.setIsDeleted(0);// 默认是0,表示未删除
        sysUserInfoMapper.insert(newSysUserInfo);
    }

    /*密码加密*/
    private String encryptPassword(String password) {
        return DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
    }
}
